package http

import (
	"dbagent/internal/conf"
	"dbagent/pkg/util"
	"fmt"
	"strings"

	bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
)

// New new a bm server.
func New() (engine *bm.Engine) {
	engine = bm.DefaultServer(conf.Conf.BM)

	util.AllowIps, _ = parseAllowIps(conf.Conf.App.AllowedIp)

	initRouter(engine)
	if err := engine.Start(); err != nil {
		panic(err)
	}
	return
}

func parseAllowIps(allowedIP []string) ([]util.IPInfo, error) {
	var allowips []util.IPInfo
	fmt.Println(allowedIP)
	for _, ipStr := range allowedIP {
		ipStr = strings.TrimSpace(ipStr)
		if len(ipStr) == 0 {
			continue
		}
		ipInfo, err := util.ParseIPInfo(ipStr)
		if err != nil {
			return nil, err
		}
		allowips = append(allowips, ipInfo)
	}
	return allowips, nil
}
